/*
 * Copyright 2009-2009 Tanesha Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *    	 this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *    	 this list of conditions and the following disclaimer in the documentation 
 *    	 and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those 
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Tanesha Project.
 */
package com.google.api.chart;

import java.util.List;

import com.google.api.chart.encoding.JoinHelper;
import com.google.api.chart.features.AxisAndLabelFeature;
import com.google.api.chart.features.BarFeature;
import com.google.api.chart.features.ColorFeature;
import com.google.api.chart.features.FillAreaFeature;
import com.google.api.chart.features.FillFeature;
import com.google.api.chart.features.FinancialMarkerFeature;
import com.google.api.chart.features.GridLineFeature;
import com.google.api.chart.features.LegendFeature;
import com.google.api.chart.features.LineStylesFeature;
import com.google.api.chart.features.RangeMarkerFeature;
import com.google.api.chart.features.ScalingFeature;
import com.google.api.chart.features.ShapeMarkerFeature;
import com.google.api.chart.features.TitleFeature;


public final class BarChartMaker implements ChartTypeMaker, BarFeature<BarChartMaker>, ColorFeature<BarChartMaker>, 
	FillFeature<BarChartMaker>, ScalingFeature<BarChartMaker>, TitleFeature<BarChartMaker>, LegendFeature<BarChartMaker>,
	AxisAndLabelFeature<BarChartMaker>, GridLineFeature<BarChartMaker>, ShapeMarkerFeature<BarChartMaker>, RangeMarkerFeature<BarChartMaker>,
	LineStylesFeature<BarChartMaker>, FillAreaFeature<BarChartMaker>, FinancialMarkerFeature<BarChartMaker> {
	
	private String orientation = "h";
	private String layout = "s";
	private String type = null;
	private double[] zerolinePercent = null;

	private CommonFeatureHelper<BarChartMaker> common;
	private AxisGridShapeRangeFeatureHelper<BarChartMaker> other;
	private LineStyleFillAreaFeatureHelper<BarChartMaker> last;
	private String chm = null;
	
	protected BarChartMaker() {
		common = new CommonFeatureHelper<BarChartMaker>(this);
		other = new AxisGridShapeRangeFeatureHelper<BarChartMaker>(this);
		last = new LineStyleFillAreaFeatureHelper<BarChartMaker>(this);
	}

	public BarChartMaker grouped() {
		layout = "g";
		return this;
	}

	public BarChartMaker stacked() {
		layout = "s";
		return this;
	}

	public BarChartMaker horizontal() {
		orientation = "h";
		return this;
	}

	public BarChartMaker vertical() {
		orientation = "v";
		return this;
	}

	@Override
	public BarChartMaker autosize(double barSpacing, double groupSpacing) {
		type = "a," + JoinHelper.doubleFormat(barSpacing) + "," + JoinHelper.doubleFormat(groupSpacing);
		return this;
	}
	
	@Override
	public BarChartMaker autosize() {
		this.type = "a";
		return this;
	}
	
	@Override
	public BarChartMaker fixed(double width, double barSpacing, double groupSpacing) {
		type = JoinHelper.doubleFormat(width) + "," +
			JoinHelper.doubleFormat(barSpacing) + "," +
			JoinHelper.doubleFormat(groupSpacing);
		return this;
	}
	
	@Override
	public BarChartMaker relative(double barSpacing, double groupSpacing) {
		type = "r," +
			JoinHelper.doubleFormat(barSpacing) + "," +
			JoinHelper.doubleFormat(groupSpacing);
		return this;
	}
	
	@Override
	public BarChartMaker zerolines(double... percents) {
		for (double percent : percents)
			if (percent < 0.0 || percent > 1.0)
				throw new IllegalArgumentException("must be 0.0 < p < 1.0, got p = " + percent);
		zerolinePercent = percents;
		return this;
	}

	@Override
	public String toString() {
		return "b" + orientation + layout;
	}
	
	@Override
	public void fillParameters(List<String> parameters, List<String> chmParameters) {
		if (zerolinePercent != null)
			parameters.add("chp=" + JoinHelper.join(zerolinePercent, ","));
		if (type != null)
			parameters.add("chbh=" + type);
		if (chm != null)
			parameters.add("chm=" + chm);
		
		common.fillParameters(parameters, chmParameters);
		other.fillParameters(parameters, chmParameters);
		last.fillParameters(parameters, chmParameters);
	}

	@Override
	public BarChartMaker colors(ColorMaker... colors) {
		return common.colors(colors);
	}

	@Override
	public BarChartMaker fills(FillMaker... fills) {
		return common.fills(fills);
	}

	@Override
	public BarChartMaker scaling(ScalingMaker... scaling) {
		return common.scaling(scaling);
	}

	@Override
	public BarChartMaker title(String... title) {
		return common.title(title);
	}

	@Override
	public BarChartMaker title(ColorMaker color, int textSize, String... title) {
		return common.title(color, textSize, title);
	}

	@Override
	public BarChartMaker legend(String... legends) {
		return common.legend(legends);
	}

	@Override
	public BarChartMaker legend(ChartLegendMaker legend) {
		return common.legend(legend);
	}

	@Override
	public BarChartMaker axes(AxisMaker... axes) {
		return other.axes(axes);
	}
	
	@Override
	public BarChartMaker dataLabels(DataLabelMaker... dataLabels) {
		return other.dataLabels(dataLabels);
	}

	@Override
	public BarChartMaker linestyles(LineStyleMaker... linestyles) {
		return last.linestyles(linestyles);
	}

	@Override
	public BarChartMaker fillAreas(FillAreaMaker... fills) {
		return last.fillAreas(fills);
	}
	
	@Override
	public BarChartMaker gridlines(GridLineMaker gridlines) {
		return other.gridlines(gridlines);
	}
	
	@Override
	public BarChartMaker shapes(ShapeMarkerMaker... shapes) {
		return other.shapes(shapes);
	}
	
	@Override
	public BarChartMaker ranges(RangeMarkerMaker... ranges) {
		return other.ranges(ranges);
	}
	
	@Override
	public BarChartMaker financial(FinancialMarkerMaker... financials) {
		this.chm = JoinHelper.join(financials, "|");
		return this;
	}
	
	@Override
	public BarChartMaker margins(MarginMaker margins) {
		return common.margins(margins);
	}
}
